/**
 * @file lavas config
 * @author BQ-DN-XSQ-349(flower.cheng@boqii.com)
 */

'use strict';
const routers = require('./core/router.js')
const path = require('path');
const BUILD_PATH = path.resolve(__dirname, 'dist');
const isDev = process.env.NODE_ENV === 'development';
const isProd = process.env.NODE_ENV === 'production';
const Config = require('webpack-chain');
const config = new Config();
// const { merge } = config;
const apiHost = require('./config/env')[process.env.fmt_env].apiHost
const nodeHost = require('./config/env')[process.env.fmt_env].nodeHost
console.log('----isDev------:', isDev, apiHost)
const name = process.env.npm_package_name
const version = process.env.npm_package_version
const versionCode = process.env.npm_package_versionCode
const build = process.env.npm_package_build
const webpack = require('webpack')

function merge(ori, des) {
  return Object.assign({}, ori, des);
}
console.log('-----fmt_env------:', process.env.fmt_env, process.env.build)

module.exports = {
  build: {
    ssr: false,
    path: BUILD_PATH,
    publicPath: isDev ? '/' : 'https://h5.boqiicdn.com/wap-community/',
    ssrCopy: isDev ? [] : [{
        src: 'server.prod.js'
      },
      {
        src: 'package.json'
      }
    ],
    /**
     * alias for webpack
     *
     * @type {Object.<string, Object>}
     */
    alias: {
      server: {
        'iscroll/build/iscroll-lite$': path.join(__dirname, 'core/iscroll-ssr.js')
      },
      base: {
        'components': path.join(__dirname, 'components'),
        'boqii-node-auth': path.join(__dirname, 'assets/lib/boqii-node-auth/'),
      }
    },
    /**
     * node externals
     *
     * @type {Array.<string|RegExp>}
     */
    nodeExternalsWhitelist: [
      /iscroll/
    ],
    // extendWithWebpackChain: (config, {type, env}) => {
    //   // 扩展 babel-loader，添加一个 babel 插件
    //   config.module
    //       .rule('js')
    //           .use('babel')
    //           .tap(options => merge(options, { plugins: ['babel-plugin-syntax-object-rest-spread'] }));
    //  },
    extendWithWebpackChain: (config, {
      type,
      env
    }) => {
      // 扩展 babel-loader，添加一个 babel 插件
      config.module
        .rule('compile')
        .test(/\.js$/)
        .pre()
        .include
        .add('pages')
        .add('service')
        .end()
        .use('babel')
        .loader('babel-loader')
        .options({
          presets: [
            ['@babel/preset-env']
          ]
        });
    },
    defines: {
      base: {},
      client: {
        'API_URL': isDev ? JSON.stringify('/phpProxy/apidemo.php') : JSON.stringify(apiHost),
        'NODE_API_URL': isDev ? JSON.stringify('/nodeProxy') : JSON.stringify(nodeHost),
        'FMT_NAME': `'${name}'`,
        'FMT_VERSION': `'${version}'`,
        'FMT_VERSION_CODE': versionCode,
        'FMT_BUILD': build,
        'ENV': JSON.stringify(process.env.fmt_env)
      },
      server: {}
    },
    plugins: {
      base: [],
      client: process.env.build ? [
        new webpack.BannerPlugin({
          banner: new Date().toLocaleString()
        }),
        new webpack.optimize.UglifyJsPlugin({
          compress: {
            warnings: false,
            drop_debugger: true,
            drop_console: true
          }
        })
      ] : [],
      server: []
    }
  },
  router: {
    mode: 'history',
    base: '/',
    pageTransition: {
      type: 'slide',
      transitionClass: 'slide'
    },
    routes: routers,
  },
  serviceWorker: {
    // If true, generate service-worker.js and sw-register.js
    // Default to `false`
    enable: false,

    // Path of service worker template
    swSrc: path.join(__dirname, 'core/service-worker.js'),

    // Path of generated service worker file
    swDest: path.join(BUILD_PATH, 'service-worker.js'),

    swPath: '/community',

    // If true, `workbox.routing.registerNavigationRoute()` won't be generated
    // Defaults to `false`
    disableGenerateNavigationRoute: true,

    // URL of appshell page
    // ONLY works in SSR mode
    appshellUrl: '/appshell'
  },
  middleware: {
    all: [],
    client: ['intercept-middleware', 'router-middleware', 'auth-middleware'],
    server: []
  },
  skeleton: {
    routes: [{
        path: '/community/demo',
        skeletonId: 'demoSkeleton',
        componentPath: 'components/skeleton/DemoSkeleton.vue'
      },
      {
        path: '*',
        componentPath: 'components/skeleton/Skeleton.vue'
      }
    ]
  }
};
